import csv
from pathlib import Path


def process_csv_files(input_dir, output_file):
    seen_records = set()

    with open(output_file, "w", newline="", encoding="utf-8") as out_f:
        writer = None
        common_fields = None

        # 第一遍：收集所有可能的字段
        for csv_file in Path(input_dir).glob("*.csv"):
            with open(csv_file, "r", encoding="utf-8") as in_f:
                reader = csv.DictReader(in_f)
                if common_fields is None:
                    common_fields = set(reader.fieldnames)
                else:
                    common_fields.update(reader.fieldnames)

        # 确保关键字段存在
        required_fields = {"点位名称", "点位位置名称", "点位坐标"}
        if not required_fields.issubset(common_fields):
            missing = required_fields - common_fields
            raise ValueError(f"缺少必要字段: {missing}")

        # 第二遍：实际处理数据
        for csv_file in Path(input_dir).glob("*.csv"):
            with open(csv_file, "r", encoding="utf-8") as in_f:
                reader = csv.DictReader(in_f)

                if writer is None:
                    # 只写入我们关心的字段（过滤掉Unnamed列）
                    fieldnames = [
                        f for f in reader.fieldnames if not f.startswith("Unnamed")
                    ]
                    writer = csv.DictWriter(out_f, fieldnames=fieldnames)
                    writer.writeheader()

                for row in reader:
                    # 过滤掉Unnamed列
                    filtered_row = {
                        k: v for k, v in row.items() if not k.startswith("Unnamed")
                    }

                    record_key = (
                        filtered_row.get("点位名称", ""),
                        filtered_row.get("点位位置名称", ""),
                        filtered_row.get("点位坐标", ""),
                    )

                    if record_key not in seen_records:
                        seen_records.add(record_key)
                        writer.writerow(filtered_row)


if __name__ == "__main__":
    input_directory = "output_utf8"  # 替换为你的输入目录
    output_file = "merged_output.csv"  # 输出文件名

    process_csv_files(input_directory, output_file)
    print(f"处理完成，结果已保存到 {output_file}")
